(function() {
	var $ = require(L('$')), _ = require(L('_'));

	var win = $.createWindow(L('win_register'));
	win.add(new $.ui('navbar')(true, false));
	var ui = $.layout('register');
	win.add(ui.view);

	var txtFirstName = $.ui('text_field').create(L('hint_first_name'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtLastName = $.ui('text_field').create(L('hint_last_name'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtProfileName = $.ui('text_field').create(L('hint_profile_name'), false, false);
	var txtEmail = $.ui('text_field').create(L('hint_email'));
	var txtPassword = $.ui('text_field').create(L('hint_password'), false, true);
	var txtConfirmPassword = $.ui('text_field').create(L('hint_confirm_password'), false, true);
	var txtShareCode = $.ui('text_field').create(L('hint_share_code'), false, false);
	ui.container.add([txtFirstName, txtLastName, txtProfileName, txtEmail, txtPassword, txtConfirmPassword, txtShareCode]);
	ui.view.add($.icon_info());

	$.tap(ui.btn_submit, function() {
		var error = '', first_name = txtFirstName.val(), last_name = txtLastName.val(), email = txtEmail.val(), password = txtPassword.val(), confirm_password = txtConfirmPassword.val(), share_code = txtShareCode.val();
		if ($.isNull(first_name))
			error = L('err_first_name_required');
		else if ($.isNull(last_name))
			error = L('err_last_name_required');
		else if ($.isNull(email) || !$.isValidEmail(email))
			error = L('err_email_required');
		else if (password.length < 6 || !$.has_number(password))
			error = L('err_password_required');
		else if (confirm_password != password)
			error = L('err_password_not_match');
		if (!$.isNull(error))
			$.a(error, L('message_error_title'));
		else {
			$.repeat(function() {
				ui.view.height = $.mainContentHeight(true);
				ui.view.scrollTo(0, 0);
			}, 5);
			$.mask();
			$.webservice().validateRegisterData(email, share_code, function(e) {
				$.mask(false);
				var result = JSON.parse(e);
				if (!result.Success) {
					if (result.ErrorCode == 0)
						$.a(result.Error, 'Email address');
					else {
						$.a(result.Error, L('message_error_title'));
						txtShareCode.val('');
					}
				} else
					$.ui('popup').terms_and_conditions().display(function() {
						$.mask();
						$.webservice().register(first_name, last_name, email, password, share_code, txtProfileName.val(), function(e) {
							$.mask(false);
							var result = JSON.parse(e);
							if (!result.Success) {
								$.a(result.Error, L('message_error_title'));
							} else {
								$.soft_indicator().show(L('message_register_success'));
								$.currentUserID(result.UserID);
								$.currentUserEmailAddress(result.EmailAddress);
								$.currentUserName(result.UserName);
								$.allowReceiveNotification(result.AllowReceiveRetailerNotification);
								$.badgeNotification(result.UnreadNotificationNumber);
								$.badgeShoppingBag(result.CheckoutNotificationNumber);
								_.delay(function() {
									$.soft_indicator().hide();
									$.openTabgroupAndFocusToMyDesires();
								}, 1500);
							}
						});
					});
			});
		}
	});
	win.reset = function() {
		$.emptyTextFields([txtFirstName, txtLastName, txtProfileName, txtEmail, txtPassword, txtConfirmPassword, txtShareCode]);
	};

	$.on('focus', win, function() {
		$.ui('text_field').init([txtFirstName, txtLastName, txtProfileName, txtEmail, txtPassword, txtConfirmPassword, txtShareCode], ui.view, $.mainContentHeight(true));
	});
	module.exports = win;
})();
